package com.bjpowernode.pay.controller;

import com.bjpowernode.pay.service.AlipayService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Controller
public class AlipayController {

    @Resource
    private AlipayService alipayService;

    @PostMapping("/place/alipay")
    @ResponseBody
    public String placeAlipay(@RequestParam("channel") String channel,
                              @RequestParam("money") String  money,
                              @RequestParam("uid") Integer uid){

        System.out.println("channel="+channel+",uid="+uid+",money="+money);
        String form = alipayService.pagePay(uid,money,channel);
        return "<form></form>";
    }
    /**
     * 接收支付宝的notify通知（异步通知）
     */
    @RequestMapping("/notify/alipay")
    public void notifyAlipay(HttpServletResponse response){

        PrintWriter out =null;
        try {
            out=response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        out.println("success");
        out.flush();
        out.close();
    }
}
